package com.jxd.petMgr.dao.impl;


import com.jxd.petMgr.dao.IPetOwnerDao;
import com.jxd.petMgr.model.PetOwner;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lzh
 * @version 1.0
 * @Description
 * @date 2021/11/3
 */


public class PetOwnerDaoImpl extends BaseDao implements IPetOwnerDao {
//    IPetStoreDao petStoreDao = new PetStoreDaoImpl();
//    IPetDao petDao = new PetDaoImpl();
//    IAccountDao accountDao = new AccountDaoImpl();


    /**
     * 获得所有主人对象
     *
     * @return 主人对象集合
     */
    @Override
    public List<PetOwner> getAllOwenr() {
        List<PetOwner> list = new ArrayList<>();
        conn = getConn();
        String sql = "select id, name , password ,money from petowner";
        try {
            pstm = conn.prepareStatement(sql);
            rs = pstm.executeQuery();
            while (rs.next()) {
                PetOwner petOwner = new PetOwner(rs.getInt("id"), rs.getString("name"),
                        rs.getString("password"), rs.getDouble("money"));
                list.add(petOwner);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(conn, pstm, rs);
        }
        return list;
    }

    /**
     * 传入主人对象更新主人表
     *
     * @param petOwner 主人对象
     * @return 是否更新成功
     */
    @Override
    public boolean updatePetOwner(PetOwner petOwner, Connection conn,PreparedStatement pstm) throws SQLException {
        boolean result;
        String sql = "update petowner set name = ? , password = ? , money = ? where id = ?";
        pstm = conn.prepareStatement(sql);
        pstm.setString(1, petOwner.getName());
        pstm.setString(2, petOwner.getPassword());
        pstm.setDouble(3, petOwner.getMoney());
        pstm.setInt(4, petOwner.getId());
        result = pstm.executeUpdate() > 0;
        return result;
    }

    /**
     * 输入用户名和密码返回主人id
     * 找不到则返回null
     *
     * @param name     用户名
     * @param password 密码
     * @return 主人id
     */
    @Override
    public int getOwnerID(String name, String password) {
        conn = getConn();
        int id = 0;//主人id
        try {
            //根据用户名和密码查找id
            String sql = "select id from petowner where name = ? and password = ?";
            pstm = conn.prepareStatement(sql);
            pstm.setString(1, name);
            pstm.setString(2, password);
            rs = pstm.executeQuery();
            while (rs.next()) {
                id = rs.getInt("id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(conn, pstm, rs);
        }
        return id;
    }

    /**
     * 根据宠物主人id获得宠物主人对象
     *
     * @param id 宠物主人id
     * @return 宠物主人对象
     */
    @Override
    public PetOwner getOwnerByID(int id) {
        conn = getConn();
        PetOwner petOwner = null;
        String sql = "select id, name , password ,money from petowner where id = ?";
        try {
            pstm = conn.prepareStatement(sql);
            pstm.setInt(1, id);
            rs = pstm.executeQuery();
            while (rs.next()) {
                petOwner = new PetOwner(rs.getInt("id"), rs.getString("name"),
                        rs.getString("password"), rs.getDouble("money"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(conn, pstm, rs);
        }
        return petOwner;
    }

//    /**
//     * 传入四个对象同时修改四张表
//     *
//     * @param pet      宠物对象
//     * @param petOwner 主人对象
//     * @param petStore 商店对象
//     * @param account  账单对象
//     */
//    @Override
//    public void changeFourSet(Pet pet, PetOwner petOwner, PetStore petStore, Account account) {
//        conn = getConn();
//        try {
//            //关闭自动提交
//            conn.setAutoCommit(false);
//
//            petDao.updatePet(pet,conn);
//            updatePetOwner(petOwner,conn);
//            petStoreDao.updatePetStore(petStore,conn);
//            accountDao.insertAccount(account,conn);
//            conn.commit();
//        } catch (SQLException e) {
//            e.printStackTrace();
//            try {
//                conn.rollback();
//            } catch (SQLException ex) {
//                ex.printStackTrace();
//            }
//        }finally {
//            close(conn,pstm,rs);
//        }
//    }
}
